class Ball {
  constructor(x, y, radius, imageIndex, propsHeight, propsWidth, ballImages) {
    this.x = x;
    this.y = y;
    this.radius = radius; // 绘制时的半径
    this.collisionRadius = radius * 0.9; // 碰撞时的半径，可以设置为小于 radius 的值
    this.imageIndex = imageIndex;
    this.velocityY = 0;
    this.velocityX = (Math.random() * 0.8 - 0.4) * 0.5;
    this.gravity = 0.15;
    this.friction = 0.85;
    this.bounce = 0.4;
    this.active = true;
    this.opacity = 1;
    this.fadeSpeed = 0.03;
    this.propsHeight = propsHeight;
    this.propsWidth = propsWidth;
    this.ballImages = ballImages;
  }

  update(balls) {
    if (!this.active) return;

    // 调整停止判定阈值
    if (this.y + this.radius >= this.propsHeight && Math.abs(this.velocityY) < 0.3) {
      this.opacity -= this.fadeSpeed;
      if (this.opacity <= 0) {
        this.active = false;
        return;
      }
    }

    // 减缓速度增加
    this.velocityY += this.gravity;

    // 增加实际运动速度的比例
    this.x += this.velocityX * 0.1;
    this.y += this.velocityY * 0.1;

    // 碰撞检测
    balls.forEach(ball => {
      if (ball !== this && ball.active) {
        const dx = ball.x - this.x;
        const dy = ball.y - this.y;
        const distance = Math.sqrt(dx * dx + dy * dy);

        if (distance < this.collisionRadius + ball.collisionRadius) { // 使用 collisionRadius 进行碰撞检测
          const angle = Math.atan2(dy, dx);
          const sin = Math.sin(angle);
          const cos = Math.cos(angle);

          const rotatedVelocityX1 = this.velocityX * cos + this.velocityY * sin;
          const rotatedVelocityY1 = this.velocityY * cos - this.velocityX * sin;
          const rotatedVelocityX2 = ball.velocityX * cos + ball.velocityY * sin;
          const rotatedVelocityY2 = ball.velocityY * cos - ball.velocityX * sin;

          this.velocityX = rotatedVelocityX2 * cos - rotatedVelocityY1 * sin;
          this.velocityY = rotatedVelocityY2 * cos + rotatedVelocityX1 * sin;
          ball.velocityX = rotatedVelocityX1 * cos - rotatedVelocityY2 * sin;
          ball.velocityY = rotatedVelocityY1 * cos + rotatedVelocityX2 * sin;

          // 分离球体，防止粘在一起
          const overlap = (this.collisionRadius + ball.collisionRadius - distance) / 2;
          const moveX = overlap * Math.cos(angle);
          const moveY = overlap * Math.sin(angle);
          this.x -= moveX;
          this.y -= moveY;
          ball.x += moveX;
          ball.y += moveY;
        }
      }
    });

    // 边界检查
    if (this.x - this.radius < 0) {
      this.x = this.radius;
      this.velocityX = -this.velocityX * this.bounce;
    }
    if (this.x + this.radius > this.propsWidth) {
      this.x = this.propsWidth - this.radius;
      this.velocityX = -this.velocityX * this.bounce;
    }
    if (this.y + this.radius > this.propsHeight) {
      this.y = this.propsHeight - this.radius;
      this.velocityY = -this.velocityY * this.bounce;
      this.velocityX *= this.friction;
    }
  }

  draw(ctx) {
    if (!this.active) return;
    
    ctx.save();
    ctx.globalAlpha = this.opacity;
    ctx.drawImage(
      this.ballImages[this.imageIndex],
      this.x - this.radius,
      this.y - this.radius,
      this.radius * 2, // 图片大小
      this.radius * 2
    );
    ctx.restore();
  }
}

export default Ball
